﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zyconfig;

namespace zyui
{
    public class LtSettings
    {

        public static double GetCqfk(string dztm,string btm)
        {
            double qfk = 0.00;
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.ConditionExpress = "读者条码='" + dztm + "'";
                string dwdm = _open.GetField("读者库", "单位代码").ToString().Trim();
                string dzjb = _open.GetField("读者库", "级别代码").ToString().Trim();
                _open.Reset();
                _open.SqlCmd = "select * from 流通库 where 应归还时间<'" + DateTime.Now + "' and 读者条码='" + dztm + "' and 条形码='"+ btm +"' order by 应归还时间";
                DataTable dr = _open.GetDataTable();
                DateTime dtime = DateTime.Now;
                foreach (DataRow row in dr.Rows)
                {
                    string xnks = row["虚拟库室"].ToString().Trim();
                    _open.Reset();
                    _open.ConditionExpress = "虚拟库室='" + xnks + "' and 单位代码='" + dwdm + "' and 级别代码='" + dzjb + "'";
                    double jsdw = Convert.ToDouble(_open.GetField("流通参数定义", "超期处罚计算单位").ToString().Trim());
                    double fkje = Convert.ToDouble(_open.GetField("流通参数定义", "超期罚款金额").ToString().Trim());
                    double dje = jsdw * fkje;
                    DateTime yght = Convert.ToDateTime(row["应归还时间"]);
                    if (yght < dtime)
                    {
                        DateTime time1 = yght;
                        List<DateTime> li = new List<DateTime>();
                        while (time1 <= dtime)
                        {
                            li.Add(time1);
                            //Console.WriteLine(time1.ToString("yyyy-MM-dd") + ":" );
                            time1 = time1.AddDays(1);
                        }
                        //DateTime[] timearr = li.ToArray();
                        foreach (DateTime dtm in li)
                        {
                            _open.Reset();
                            _open.ConditionExpress = "日期='" + DateTime.Parse(dtm.ToString("yyyy-MM-dd")) + "'";
                            string clgq = _open.GetField("流通工作日", "处理过期文献").ToString().Trim();
                            if (clgq.Equals("Y"))
                            {
                                qfk += dje;
                                Console.WriteLine(dztm + ":" + dtm.ToString("yyyy-MM-dd") + ":" + clgq + ":" + qfk);
                            }
                        }
                    }
                }
            }
            return qfk;
        }
        public static void UpdateCq()
        {
            using (DbOperHandler _open = new Common().Doh())
            {
                _open.Reset();
                _open.SqlCmd = "select 读者条码 from 流通库 where 应归还时间<'"+ DateTime.Now+"' group by 读者条码 order by 读者条码";
                DataTable dt = _open.GetDataTable();
                foreach(DataRow drow in dt.Rows)
                {
                    string dztm = drow["读者条码"].ToString().Trim();
                    _open.Reset();
                    _open.ConditionExpress = "读者条码='" + dztm + "'";
                    string dwdm = _open.GetField("读者库", "单位代码").ToString().Trim();
                    string dzjb = _open.GetField("读者库", "级别代码").ToString().Trim();
                    double qfk = 0.00;
                    _open.Reset();
                    _open.SqlCmd = "select * from 流通库 where 应归还时间<'" + DateTime.Now + "' and 读者条码='" + dztm + "' order by 应归还时间";
                    DataTable dr = _open.GetDataTable();
                    DateTime dtime = DateTime.Now;
                    foreach (DataRow row in dr.Rows)
                    {
                        string xnks = row["虚拟库室"].ToString().Trim();
                        _open.Reset();
                        _open.ConditionExpress = "虚拟库室='" + xnks + "' and 单位代码='" + dwdm + "' and 级别代码='" + dzjb + "'";
                        double jsdw = Convert.ToDouble(_open.GetField("流通参数定义", "超期处罚计算单位").ToString().Trim());
                        double fkje = Convert.ToDouble(_open.GetField("流通参数定义", "超期罚款金额").ToString().Trim());
                        double dje = jsdw * fkje;
                        DateTime yght = Convert.ToDateTime(row["应归还时间"]);
                        if (yght < dtime)
                        {
                            DateTime time1 = yght;
                            List<DateTime> li = new List<DateTime>();
                            while (time1 <= dtime)
                            {
                                li.Add(time1);
                                //Console.WriteLine(time1.ToString("yyyy-MM-dd") + ":" );
                                time1 = time1.AddDays(1);
                            }
                            //DateTime[] timearr = li.ToArray();
                            foreach (DateTime dtm in li)
                            {
                                _open.Reset();
                                _open.ConditionExpress = "日期='" + DateTime.Parse(dtm.ToString("yyyy-MM-dd")) + "'";
                                string clgq = _open.GetField("流通工作日","处理过期文献").ToString().Trim();
                                if (clgq.Equals("Y"))
                                {
                                    qfk += dje;
                                    Console.WriteLine(dztm+":"+dtm.ToString("yyyy-MM-dd") + ":" + clgq+":"+qfk);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
